package ru.niir.goodfil.utils.validation;

import java.util.ArrayList;
import java.util.Collection;

public class ValidationResult
{
    private boolean result;
    private String className;
    private final ArrayList<ValidationError> errors = new ArrayList<ValidationError>();

    private ValidationResult(boolean result, Class clas)
    {
        this.className = clas.getSimpleName();
        this.result = result;
    }

    public static ValidationResult Ok(Class clas)
    {
        return new ValidationResult(true, clas);
    }

    public static ValidationResult Error(Class clas)
    {
        return new ValidationResult(false, clas);
    }

    public static ValidationResult Error(Class clas, ValidationError error)
    {
        ValidationResult result = new ValidationResult(false, clas);
        result.addValidationError(error);
        return result;
    }

    public static ValidationResult Error(Class clas, ValidationError[] errors)
    {
        ValidationResult result = new ValidationResult(false, clas);
        for (ValidationError error : errors) result.addValidationError(error);
        return result;
    }

    public static ValidationResult Error(Class clas, Collection<ValidationError> errors)
    {
        ValidationResult result = new ValidationResult(false, clas);
        for (ValidationError error : errors) result.addValidationError(error);
        return result;
    }

    public void addValidationError(ValidationError error)
    {
        result = false;
        errors.add(error);
    }

    public String getClassName()
    {
        return className;
    }

    public boolean isOk()
    {
        return result;
    }

    public boolean isError()
    {
        return !result;
    }

    @Override
    public String toString()
    {
        if (errors.size() == 0) return "{}";
        StringBuilder sb = new StringBuilder("{");
        for (int i = 0; i < errors.size() - 1; i++) sb.append(errors.get(i).toString() + ", ");
        sb.append(errors.get(errors.size() - 1) + "}");
        return sb.toString();
    }

    public String toHumanString()
    {
        if (errors.size() == 0) return "{}";
        StringBuilder sb = new StringBuilder("{");
        for (int i = 0; i < errors.size() - 1; i++) sb.append(errors.get(i).toHumanString() + ", ");
        sb.append(errors.get(errors.size() - 1) + "}");
        return sb.toString();
    }

    public ArrayList<ValidationError> getErrors()
    {
        return errors;
    }
}
